package com.tanhua.server.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.commons.utils.JwtUtils;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.domain.BlackList;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.service.UsersService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UsersService usersService;

    @GetMapping
    public UserInfoVo users(Long userID, @RequestHeader("Authorization") String token) {
        Long userId = userID;
        if (userID == null) {
            //1. 判断token里的数据是否存在
            boolean verifyToken = JwtUtils.verifyToken(token);
            //2. 若不存在就抛出异常
            if (!verifyToken) {
                throw new RuntimeException("用户未登录");
            }
            Claims claims = JwtUtils.getClaims(token);
            userId = claims.get("id", Long.class);
        }
        UserInfoVo userInfo = usersService.findUserInfoById(userId);
        return userInfo;
    }

    /**
     * 请求头：PUT/users
     * 请求参数：UserInfo，Headers：Authorization
     * 返回值：void
     */
    @PutMapping
    public void updateUserInfo(@RequestBody UserInfo userInfo, @RequestHeader("Authorization") String token) {

        //1. 判断token里的数据是否存在
        boolean verifyToken = JwtUtils.verifyToken(token);
        //2. 若不存在就抛出异常
        if (!verifyToken) {
            throw new RuntimeException("用户未登录");
        }
        Claims claims = JwtUtils.getClaims(token);
        Long userId = claims.get("id", Long.class);

        userInfo.setId(userId);
        //3. 调用service
        usersService.updateUserInfo(userInfo);
    }

    //查询通用设置
    @GetMapping("/settings")
    public ResponseEntity settings() {
        SettingsVo vo = usersService.settings();
        return ResponseEntity.ok(vo);
    }

    //陌生人问题
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        //1. 获取参数
        String content = map.get("content").toString();

        //2. 调用service
        usersService.questions(content);
        //3. 返回
        return ResponseEntity.ok(null);
    }

    //通知设置-保存
    @PostMapping("/notifications/setting")
    public ResponseEntity setting(@RequestBody Map map) {
        //1. 获取参数
        Boolean like = (Boolean) map.get("likeNotification");
        Boolean pinglun = (Boolean) map.get("pinglunNotification");
        Boolean gonggao = (Boolean) map.get("gonggaoNotification");

        //2. 调用service
        usersService.setting(like, pinglun, gonggao);

        //3. 返回数据
        return ResponseEntity.ok(null);
    }

    //黑名单-分页
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        //1. 调用service
        PageResult vo = usersService.blacklist(page, pageSize);

        //2. 返回数据
        return ResponseEntity.ok(vo);
    }

    //删除黑名单
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity delBlacklist(@PathVariable("uid") Long blackUserId) {
        //1. 调用service
        usersService.delBlacklist(blackUserId);
        //2. 返回数据
        return ResponseEntity.ok(null);
    }

    //更新头像
    @PostMapping("/header")
    public ResponseEntity saveHeader(MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {
        //1. 判断token里的数据是否存在
        boolean verifyToken = JwtUtils.verifyToken(token);

        //2. 若不存在就抛出异常
        if (!verifyToken) {
            throw new RuntimeException("用户未登录");
        }

        //3. 若存在的话，调用方法保存
        Claims claims = JwtUtils.getClaims(token);
        Long id = claims.get("id", Long.class);
        usersService.head(headPhoto, id);
        return ResponseEntity.ok(null);
    }

}


